/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Business;

import Model.DAO;
import java.sql.PreparedStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author Nguyen Hung
 */
public class Customer {

    private String name;
    private String email;
    private String phoneNumber;
    
    private String address;

//    private int customerID;

    public Customer(String name, String email, String address, String phoneNumber) {
        this.name = name;
        this.email = email;
        this.address = address;
        this.phoneNumber = phoneNumber;
        
//        NumberFormat nf = NumberFormat.get
    }

    public Customer() {
    }



    public String getEmail() {
        return email;
    }

    public String getName() {
        return name;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }



    public void setEmail(String email) {
        this.email = email;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public void save() {
        
        try {
            Connection connection = DAO.getConnection();
            String sql = "insert into customer values (null, ?, ?, ?, ?)";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, name);
            ps.setString(2, email);
            ps.setString(3, address);
            ps.setString(4, phoneNumber);
            
            ps.execute();
            connection.close();

        } catch (SQLException ex) {
            
            ex.printStackTrace();
        }

        

    }

    public int getCustomerID() {
        int customerID = 0;
        try {

            Connection connection = DAO.getConnection();
            Statement statement = connection.createStatement();
            ResultSet set = statement.executeQuery("select customerId from customer where name = '" + name + "' and email = '" + email +"' and phonenumber = '" + phoneNumber+"'");
            while (set.next()) {
                customerID = set.getInt("customerID");
            }
            set.close();
            return customerID;
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return customerID;
    }
    
    
public boolean checkCustomerExist(){
    boolean result = false;
    
    try{
            Connection connection = DAO.getConnection();            
            Statement statement = connection.createStatement();
            ResultSet set = statement.executeQuery("select customerId from customer where name = '" + name + "' and email = '" + email +"' and phonenumber = '" + phoneNumber+"'");
            while (set.next()) {
                result = true;
            }
            set.close();
            connection.close();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    return result;
}


    public String getAddress() {
        return address;
    }
}
